<template>
  <div class="app-container">
    <el-row style="margin-top:15px">
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-button type="success" @click="addDataBtn">新增</el-button>
          <el-button type="warning" @click="moreDel">高级搜索</el-button>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="grid-content bg-purple-light">
          <el-input v-model="iptSearch" placeholder="请输入内容" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="onSearch"></el-button>
          </el-input>
        </div>
      </el-col>
    </el-row>

    <el-table
      style="width: 100%; margin: 10px auto"
      @selection-change="handleSelectionChange"
      @sort-change="onSortChange"
      height="450"
      :data="list"
    >
      <el-table-column fixed type="index" label="#" width="50"></el-table-column>
      <el-table-column fixed prop="title" label="题目" width="300"></el-table-column>
      <el-table-column prop="cid" label="分类" width="200"></el-table-column>
      <el-table-column prop="uid" label="作者" width="200"></el-table-column>

      <el-table-column prop="created" label="日期" sortable width="200"></el-table-column>
      <el-table-column fixed="right" label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="info" size="small">详情</el-button>
          <el-button type="warning" size="small" @click="upDataClk(scope.row)">修改</el-button>
          <el-button type="danger" size="small" @click="delDataClk(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 抽屉 -->
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      :before-close="handleClose"
      :with-header="true"
      size="50%"
    >
      <el-form
        :model="form"
        size="mini"
        style="padding: 0 5%"
       
        label-width="100px"
        class="demo-form"
      >
        <el-form-item label="标题" prop="desc">
          <el-input type="textarea" v-model="form.title" rows="1"></el-input>
        </el-form-item>
        <el-form-item label="类别" prop="resource">
          <el-select v-model="form.cid" placeholder="请选择类别">
            <el-option v-for="(v,i) in tableData"   :key="i" :label="v.name" :value="v.id" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选项" prop="type" class="items">
          <div class="box" v-for="(item,index) in arr" :key="index">
            <el-checkbox @change="answerFiletr" name="type" v-model="item.checked">{{item.index}}</el-checkbox>
            <el-input
              class="textarea"
              style="width:300px"
              type="textarea"
              rows="1"
              clos="15"
              v-model="item.value"
            ></el-input>

            <div v-if="arr.length-1 == index" style="margin-top:10px">
              <el-button
                type="primary"
                class="el-icon-plus"
                circle
                style="font-size:20px"
                @click="addOptions"
              ></el-button>
              <el-button
                type="primary"
                v-if="index == 3 ? false : true"
                class="el-icon-minus"
                circle
                style="font-size:20px"
                @click="reduceOptions"
              ></el-button>
            </div>
          </div>
        </el-form-item>

        <el-form-item label="答案" prop="desc">{{answerArr}}</el-form-item>

        <el-form-item label="答案提示" prop="desc">
          <el-input type="textarea" rows="1" v-model="form.tips"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addClk(form)">{{btnText}}</el-button>
          <el-button @click="cancelClk">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 分页 -->
    <!-- <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>-->
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from "vuex";

export default {
   layout: "main",
  data() {
    return {
      drawer: false,
      form: {
        uid: 1,
        cid:23
      },
      answerTishi: "",
      answerArr: [],
      type:[],
      arr: [
        { index: "A", value: "", checked: false },
        { index: "B", value: "", checked: false },
        { index: "C", value: "", checked: false },
        { index: "D", value: "", checked: false }
      ],
      title: "新增",
      btnText: "保存",
      num: 68,
      arrDel: [],
      idAs: "",
      iptSearch: "",
      isShow:true,
      
    };
  },
  created() {
    this.getQuestion(),
    this.getClassify()
  },

  methods: {
    ...mapActions('test',['getQuestion','addQuestion','delQuestion','updateQuestion']),
    ...mapActions('classify',['getClassify']),
    onSearch() {},
    // 点击添加
    addClk() {
      if (this.form.id) {
        this.updateQuestion(this.form);
      } else {
        this.addQuestion(this.form);
      }

      this.form.options = JSON.stringify(this.arr);
      this.form.answers = JSON.stringify(this.answerArr);

      // this.addQuestion(this.form);
      this.emptyFn();
      this.drawer = false;

      
      this.drawer = false;

      console.log(this.form);
    },

    // 清空的方法
    emptyFn() {
      this.form = { uid: 1 };
      (this.arr = [
        { index: "A", value: "", checked: false },
        { index: "B", value: "", checked: false },
        { index: "C", value: "", checked: false },
        { index: "D", value: "", checked: false }
      ]),
        (this.answerArr = []),
        (this.num = 68);
    },

    //增加答案选项
    addOptions() {
      this.num++;
      this.arr.push({
        index: String.fromCharCode(this.num),
        value: "",
        checked: false
      });
    },
    //减少答案选项
    reduceOptions() {
      this.num--;
      this.arr.pop();
    },

    //答案
    answerFiletr() {
      this.answerArr = [];
      let arr = this.arr;
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].checked == true) {
          this.answerArr.push(arr[i].index);
        }
      }
      // console.log(this.answerArr);
    },
    //取消点击
    cancelClk() {
      this.drawer = false;
    },
    delDataClk(value) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.delQuestion(value);
          // let sj = Math.ceil((this.total - 1) / this.pageSize);
          // this.handleCurrentChange(sj);
          this.$message({
            type: "success",
            message: "删除成功!"
          });

        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    moreDel() {},
    upDataClk(row) {
      this.drawer = true;
      this.title = "修改";
      this.form = { ...row};
      console.log(this.form);
      this.isShow = false;
    },
    addDataBtn() {
      this.drawer = true
    },
    handleSelectionChange(sid) {},
    //排序
    onSortChange(value) {},
    //抽屉关闭
    handleClose(done) {
     
    },
    // 每页几条
    async handleSizeChange(val) {},
    //当前页
    async handleCurrentChange(val) {},
    onChange(val) {}
  },
  computed: {
    ...mapState('test',['list']),...mapState('classify',['tableData'])
  
  },
  components: {
    
  }
};
</script>

<style scoped>
.textarea {
  margin-left: 12px;
}

.box {
  display: flex;
  align-items: center;
  /* justify-content: center; */
}
</style>